/**
 * This panel visualizes that a RPC activity is in progress.
 */
package org.hackystat.ui.telemetryviewer.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * This panel visualizes that a RPC activity is in progress.
 * @author David J. Nickles
 *
 */
public class WaitPanel extends Composite implements WindowResizeListener {
  
  /** {@literal} DEFAULTMESSAGE icon. */ 
  private static final String DEFAULTMESSAGE = "Loading...";

  private Image statusImage;
  private Label statusLabel;
  private String message;
  private HorizontalPanel panel;
  
  /**
   * Constructs a new WaitPanel with the given message.
   * @param message String to display or "Loading..." if null.
   */
  public WaitPanel(String message) {
    super();
    if (message == null) {
      this.message = this.DEFAULTMESSAGE;
    }
    else {
      this.message = message;
    }
    (this.panel = new HorizontalPanel()).setStyleName(Styles.BASICPANEL);
    this.panel.add(this.statusImage = new Image(Images.WAIT_IMAGE));
    this.statusImage.setSize(Styles.SMIMAGESIZE, Styles.SMIMAGESIZE); 
    this.panel.setCellHorizontalAlignment(this.statusImage, HasHorizontalAlignment.ALIGN_CENTER);
    this.panel.setCellVerticalAlignment(this.statusImage, HasVerticalAlignment.ALIGN_MIDDLE); 
    this.panel.add(this.statusLabel = new Label(this.message));
    this.statusLabel.setStyleName(Styles.SMALLLABEL);
    this.panel.setCellHorizontalAlignment(this.statusLabel, HasHorizontalAlignment.ALIGN_CENTER);
    this.panel.setCellVerticalAlignment(this.statusLabel, HasVerticalAlignment.ALIGN_MIDDLE);
    this.panel.setWidth(Integer.toString(Window.getClientWidth() - Styles.RIGHTOFFSET)
      + Styles.PIXELSUFFIX);
    Window.addWindowResizeListener(this);
    initWidget(this.panel);
  }
  
  /** 
   * Listens for user resizing of the window.
   * @param width of the adjusted window..
   * @param height of the adjusted window.
   */
  public void onWindowResized(int width, int height) {
    this.panel.setWidth(Integer.toString(width - Styles.RIGHTOFFSET) + Styles.PIXELSUFFIX);
  }
}
